package pers.hl.library;

import org.junit.jupiter.api.Test;
import pers.hl.library.utils.LogUtils;

import java.security.MessageDigest;

public class MD5Utils {

    /***
     * MD5加码 生成32位md5码
     */
    @Test
    public String string2MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            LogUtils.i(e.toString());
            e.printStackTrace();
            return "";
        }
        inStr = "abd123";
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuilder hexValue = new StringBuilder();
        for (byte md5Byte : md5Bytes) {
            int val = ((int) md5Byte) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
        String result = hexValue.toString();
        System.out.println("加密结果:" + result);
        return result;

    }
}
